package ch.gislb2.helpers;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;

import android.content.Context;
import android.os.Build;
import android.util.Log;

public class CSVWriter {
	
	public static <T> boolean writeToFile(Collection<T> data, Context context, String filepath)
	{
		try
		{
			DateFormat dfmt = new SimpleDateFormat( "yyyyMMddHHmmss" );
			String headers = "Time-Stamp;SensorFusionValue;SensorFusionAccuracy;accuracyACCELEROMETER;accuracyGyro;accuracyMAGNETIC_FIELD";
			Calendar c = new GregorianCalendar();
			String manufacturer = Build.MANUFACTURER +"_";
			String model = Build.MODEL +"_";
			 String filename = manufacturer + model + "SensorLog_"+dfmt.format(c.getTime())+".csv";

			File file = new File (filepath, filename);
	        FileOutputStream fOut1 = new FileOutputStream(file);
			OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut1);
			myOutWriter.append(headers +"\n");
			for(T value: data)
			{
				myOutWriter.append(value.toString()+"\n");
			}
			myOutWriter.close();
			fOut1.close();
	        
	        return true;
		}
        catch(Exception e)
        {
        	Log.e("DistanceCalculator", "CSVWriter Exception"+ e.getStackTrace());
        	return false;
        } 
		
	}

}
